package team.knowers.qzh.qzhgrame.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhgrame.common.convention.exception.ClientException;
import team.knowers.qzh.qzhgrame.dao.entity.CourseContentDO;
import team.knowers.qzh.qzhgrame.dao.mapper.CourseContentMapper;
import team.knowers.qzh.qzhgrame.dto.req.AddCourseContentReqDTO;
import team.knowers.qzh.qzhgrame.dto.req.DeleteCourseContentReqDTO;
import team.knowers.qzh.qzhgrame.dto.req.UpdateCourseContentReqDTO;
import team.knowers.qzh.qzhgrame.dto.resp.AllCourseContentRespDTO;
import team.knowers.qzh.qzhgrame.service.CourseContentGrameService;

import java.util.ArrayList;
import java.util.List;

import static team.knowers.qzh.qzhgrame.common.web.FourteenDigitRandomGenerator.generateFourteenDigitRandom;

@Service
@AllArgsConstructor
public class CourseContentGrameServiceImpl extends ServiceImpl<CourseContentMapper, CourseContentDO> implements CourseContentGrameService {
    @Override
    public int addCourseContent(AddCourseContentReqDTO req) {
        LambdaQueryWrapper<CourseContentDO> queryWrapper= Wrappers.lambdaQuery(CourseContentDO.class)
                .eq(CourseContentDO::getContent,req.getContent())
                .eq(CourseContentDO::getSectionId,req.getSectionId())
                .eq(CourseContentDO::getOrder,req.getOrder())
                .eq(CourseContentDO::getDelFlag,0);
        CourseContentDO courseContentDO=baseMapper.selectOne(queryWrapper);
        if(courseContentDO !=null)
        {
            throw new ClientException("段落已存在");
        }
        long a=generateFourteenDigitRandom();
        CourseContentDO bean= BeanUtil.toBean(req,CourseContentDO.class);
        bean.setId(a);
        return baseMapper.insert(bean);
    }

    @Override
    public int deleteCourseContent(Long id) {
        LambdaQueryWrapper<CourseContentDO> queryWrapper=Wrappers.lambdaQuery(CourseContentDO.class)
                .eq(CourseContentDO::getId,id)
                .eq(CourseContentDO::getDelFlag,0);
        CourseContentDO courseContentDO=baseMapper.selectOne(queryWrapper);
        courseContentDO.setDelFlag(1);

        return baseMapper.update(courseContentDO,queryWrapper);
    }

    @Override
    public int updateCourseContent(UpdateCourseContentReqDTO req) {
        LambdaQueryWrapper<CourseContentDO> queryWrapper=Wrappers.lambdaQuery(CourseContentDO.class)
                .eq(CourseContentDO::getId,req.getId())
                .eq(CourseContentDO::getSectionId,req.getSectionId())
                .eq(CourseContentDO::getDelFlag,0);
        CourseContentDO courseContentDO=baseMapper.selectOne(queryWrapper);
        courseContentDO.setContent(req.getContent());
        courseContentDO.setOrder(req.getOrder());
        LambdaUpdateWrapper updateWrapper=Wrappers.lambdaUpdate(CourseContentDO.class)
                .eq(CourseContentDO::getId,req.getId())
                .eq(CourseContentDO::getSectionId,req.getSectionId())
                .eq(CourseContentDO::getDelFlag,0);
        return baseMapper.update(courseContentDO,updateWrapper);
    }

    @Override
    public List<AllCourseContentRespDTO> shouCourseContent(Long id) {
        LambdaQueryWrapper<CourseContentDO> queryWrapper=Wrappers.lambdaQuery(CourseContentDO.class)
                .eq(CourseContentDO::getSectionId,id)
                .eq(CourseContentDO::getDelFlag,0)
                .orderByAsc(CourseContentDO::getOrder);
        List<CourseContentDO> courseContentDOS=baseMapper.selectList(queryWrapper);
        List<AllCourseContentRespDTO> allCourseContentRespDTOS=new ArrayList<>();
        for(CourseContentDO courseContentDO:courseContentDOS)
        {
            AllCourseContentRespDTO allCourseContentRespDTO=new AllCourseContentRespDTO();
            allCourseContentRespDTO.setContent(courseContentDO.getContent());
            allCourseContentRespDTO.setId(courseContentDO.getId());
            allCourseContentRespDTO.setOrder(courseContentDO.getOrder());
            allCourseContentRespDTOS.add(allCourseContentRespDTO);
        }
        return allCourseContentRespDTOS;
    }

}
